package org.k.GUI.explorer;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class Window extends JPanel {
	
	JSplitPane jspW, jspH;// 拆分窗格控件（容器类）
	private JPanel jp1,jp2,jp3;//3个显示窗口
	double h=0.8,w=0.6;
	
	public double getH() {
		return h;
	}

	public void setH(double h) {
		this.h = h;
	}

	public double getW() {
		return w;
	}

	public void setW(double w) {
		this.w = w;
	}

	public JPanel getJp1() {
		return jp1;
	}

	public void setJp1(JPanel jp1) {
		this.jp1 = jp1;
		this.repaint();//刷新组件
	}

	public JPanel getJp2() {
		return jp2;
	}

	public void setJp2(JPanel jp2) {
		this.jp2 = jp2;
		this.repaint();//刷新组件
	}

	public JPanel getJp3() {
		return jp3;
	}

	public void setJp3(JPanel jp3) {
		this.jp3 = jp3;
		this.repaint();//刷新组件
	}

	public Window(JPanel jp1,JPanel jp2,JPanel jp3) {
		this.setLayout(new BorderLayout());
		this.jp1=jp1;
		this.jp2=jp2;
		this.jp3=jp3;

		SetAction setaction=new SetAction();
		jp1.addMouseListener(setaction);
		jp2.addMouseListener(setaction);
		jp3.addMouseListener(setaction);
		
		
		//new JButton("1")new JButton("2")new JButton("3")
		jspW = new JSplitPane(JSplitPane.VERTICAL_SPLIT,jp1 ,
				jp2);
		
		jspH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jp3,
				jspW);
		
		jspW.setOneTouchExpandable(true);//可以收缩
		jspH.setOneTouchExpandable(true);//可以收缩
		this.add(jspH);
		/***** 初始化事件 ***/
		this.addComponentListener(new ComponentAdapter() {

			public void componentResized(ComponentEvent e) {
				if(flag==0){
				jspW.setDividerLocation(w);// 设定分割面板的左右比例(这时候就生效了，如果放在setVisible(true)这据之前就不会有效果。)
				jspH.setDividerLocation(h);
				}
			}
		});
		
		
	}
	short flag=0;//窗口最大化
	
	//监听本panel 内的动作
	class SetAction implements MouseListener{
		public SetAction(){
			
		}
		
		
		
		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub

			if (e.getClickCount() == 2) {
				if (e.getSource() == jp1) {
					// 右上框
					jspW.setDividerLocation(1.0);// 设定分割面板的左右比例
					jspH.setDividerLocation(0.0);
				} else if (e.getSource() == jp2) {
					// 右下框
					jspW.setDividerLocation(0.0);// 设定分割面板的左右比例
					jspH.setDividerLocation(0.0);
				} else if (e.getSource() == jp3) {
					// 左框
					// 设定分割面板的左右比例)
					jspH.setDividerLocation(1.0);
				}

				if (flag == 1) {
					jspW.setDividerLocation(0.6);// 设定分割面板的左右比例(这时候就生效了，如果放在setVisible(true)这据之前就不会有效果。)
					jspH.setDividerLocation(0.8);
				}

				if (flag == 0)
					flag = 1;
				else
					flag = 0;
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

	
	}
	
	public static void main(String[] args) {

		JFrame jf = new JFrame();
		// ImageIcon icon=new ImageIcon("1.jpg");
		
		Window win = new Window(new JPanel(),new JPanel(),new JPanel());

		jf.add(win);
		jf.setSize(800,400 );
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
